package gradle.common.core.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;


/**
 * java工具类
 */
@Slf4j
public class JavaUtil {

    /**
     * maximum of 2000 parameters
     */
    public static final Integer TWO_THOUSAND = 2000;

    /**
     * 分页遍历处理
     *
     * @param elements 集合
     * @param pageSize 分页大小
     * @param action   操作
     * @param <T>      泛型
     */
    public static <T> void pageForEach(List<T> elements, int pageSize, Consumer<List<T>> action) {
        if (null == elements || elements.isEmpty() || null == action) {
            return;
        }
        if (pageSize < 1) {
            throw new RuntimeException("the parameter pageSize cannot be less than 1");
        }
        int index = 0, toIndex = 0, listSize = elements.size();
        do {
            toIndex = Math.min(listSize, toIndex + pageSize);
            List<T> subList = elements.subList(index * pageSize, toIndex);
            action.accept(subList);
            index++;
        } while (toIndex < listSize);
    }

    /**
     * 分页处理列表，并返回总结果列表
     *
     * @param elements 列表元素
     * @param pageSize 每页的大小
     * @param action   处理每个子列表的函数
     * @param <T>      列表元素的类型
     * @param <R>      处理结果的类型
     * @return 处理后的总结果列表
     */
    public static <T, R> List<R> pageForEachReturn(List<T> elements, int pageSize, Function<List<T>, List<R>> action) {
        if (null == elements || elements.isEmpty() || null == action) {
            return new ArrayList<>(); // 如果列表为空或 action 为 null，返回空列表
        }
        if (pageSize < 1) {
            throw new RuntimeException("the parameter pageSize cannot be less than 1");
        }
        int index = 0, toIndex = 0, listSize = elements.size();
        List<R> totalResult = new ArrayList<>(); // 用于累积总结果的列表
        do {
            toIndex = Math.min(listSize, toIndex + pageSize);
            List<T> subList = elements.subList(index * pageSize, toIndex);
            List<R> subResult = action.apply(subList); // 获取 subList 的处理结果
            if (null != subResult && subResult.size() != 0) {
                totalResult.addAll(subResult); // 将子结果添加到总结果列表中
            }
            index++;
        } while (toIndex < listSize);
        return totalResult; // 返回累积的总结果列表
    }

    /**
     * 实体转map
     */
    public static Map<String, Object> objectToMap(Object obj) {
        Class<?> type = obj.getClass();
        Map<String, Object> returnMap = new HashMap<>();
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(type);
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor descriptor : propertyDescriptors) {
                String propertyName = descriptor.getName();
                if ("class".equals(propertyName)) {
                    continue;
                }
                Method readMethod = descriptor.getReadMethod();
                Object result = readMethod.invoke(obj);
                if (result != null) {
                    returnMap.put(propertyName, result);
                }
            }
        } catch (IntrospectionException | IllegalAccessException | InvocationTargetException e) {
            log.error("实体转Map异常, exception:{}", ExceptionUtils.getStackTrace(e));
        }
        return returnMap;
    }

}
